package vip.xiaonuo.vein.websocket;

import com.alibaba.fastjson.JSON;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.vein.service.sn.RedisSnGenerate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
public class SendTestController {


    @Autowired
    private WebSocket webSocket;

    @Resource
    private RedissonClient client;

    @Autowired
    private RedisSnGenerate redisGenerate;

    @GetMapping("sendMessage")
    public String send(String text){
        webSocket.sendAllMessage(text);
        return "ok";
    }

    @GetMapping("/sendMessage/getSnNo")
    public List<String> getSnNo(String deviceCode,int num){
        List<String> result = new ArrayList<>();
        for(int i = 0 ; i < num ; i++){
            result.add(redisGenerate.getSnNo(deviceCode));
        }
        return result;
    }

    @GetMapping("/sendMessage/testRedisson")
    public String testRedisson(){
        RBucket<List<Integer>> bucket = client.getBucket("test");
        List<Integer> list = new ArrayList<>();
        if(!bucket.isExists()){
            list.add(0);
            list.add(0);
            list.add(0);
            list.add(0);
        }else{
            Random random = new Random();
            list.add(random.nextInt());
            list.add(random.nextInt());
            list.add(random.nextInt());
            list.add(random.nextInt());
        }
        bucket.set(list);

        list = bucket.get();
        return JSON.toJSONString(list);
    }

    @GetMapping("/sendMessage/getOneSnNo")
    public String getOneSnNo(String deviceCode){
        return redisGenerate.getSnNo(deviceCode);
    }
}
